﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listWebhooksOutput : Api_BaseOutput
   {
      public List<listWebhooksResults> result { get; set; }
      public listWebhooksParms inputParms { get; private set; }
      // --------------------------------------------------------
      public listWebhooksOutput( listWebhooksInput input )
         : base( input )
      {
         result = new List<listWebhooksResults>();
         inputParms = input.parms;
      }
      // --------------------------------------------------------
      public override string ToString()
      {
         return string.Format( "List Id: {0}, Webhooks: {1}",
            this.inputParms.id, result.Count );
      }
      #region ================================================================== format setup
      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // --------------------------------------------------------------------
      // ---- xmlrpc
      internal void Format( x_listWebhook[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }
      #region ================================================================= Output
      // ---------------------------------------------------------- outputXML
      protected void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            IEnumerable<XElement> entries = xml.Elements( "struct" );
            foreach ( XElement entry in entries )
            {
               listWebhooksResults result = new listWebhooksResults();

               result.url = entry.Element( "url" ).Value;
               //
               IEnumerable<XElement> actions = entry.Element( "actions" ).Descendants();
               foreach ( XElement action in actions )
               {
                  EnumValues.listWebhookActions act =
                    apiHelper.GetEnumFromString<EnumValues.listWebhookActions>( action.Name.ToString() );
                  bool bt = ( action.Value == "1" ) ? true : false;
                  result.actions.Add( act, bt );
               }
               //
               IEnumerable<XElement> sources = entry.Element( "sources" ).Descendants();
               foreach ( XElement source in sources )
               {
                  EnumValues.listWebhookSources src =
                     apiHelper.GetEnumFromString<EnumValues.listWebhookSources>( source.Name.ToString() );
                  bool bt = ( source.Value == "1" ) ? true : false;
                  result.sources.Add( src, bt );
               }
               this.result.Add( result );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- outputJSON
      protected void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listWebhook[] jsonResult = jss.Deserialize<x_listWebhook[]>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // ---------------------------------------------------------- Format xml-rpc & JSON
      private List<listWebhooksResults> FormatResults( x_listWebhook[] x_results )
      {
         List<listWebhooksResults> results = new List<listWebhooksResults>();
         //
         foreach ( x_listWebhook result in x_results )
         {
            listWebhooksResults entry = new listWebhooksResults();
            //
            entry.url = result.url;

            foreach ( DictionaryEntry action in result.actions )
            {
               EnumValues.listWebhookActions act =
                  apiHelper.GetEnumFromString<EnumValues.listWebhookActions>( action.Key.ToString() );
               entry.actions.Add( act, (bool)action.Value );
            }

            foreach ( DictionaryEntry source in result.sources )
            {
               EnumValues.listWebhookSources src =
                  apiHelper.GetEnumFromString<EnumValues.listWebhookSources>( source.Key.ToString() );
               entry.sources.Add( src, (bool)source.Value );
            }

            results.Add( entry );
         }
         return results;
      }

      #endregion
      #endregion
   }
}
